﻿using System;
using System.Collections.Generic;
using System.Text;
using BarCode.Bean;
using BarCode.Common;
using System.Data;
using BarCode.Module;
using MySql.Data.MySqlClient;

namespace BarCode.AccessDao
{
    public class JXJKDBDao
    {
        private static readonly log4net.ILog LogUtil = log4net.LogManager.GetLogger(typeof(JXJKDBDao));

        private MySqlHelper db;
        public JXJKDBDao(string serverIp, string dbName, string userId, string userPwd)
        {
            db = new MySqlHelper(serverIp, dbName, userId, userPwd);
        }

        /// <summary>
        /// 判断是否可链接
        /// </summary>
        /// <returns></returns>
        public bool EnableConnect()
        {
            return db.EnableConnect();
        }

        /// <summary>
        /// 保存机芯壳二维码信息数据
        /// </summary>
        /// <param name="lstBarCode"></param>
        /// <returns>处理结果</returns>
        public int saveJKJXBarCode(JXJKBarCodeInfo jxjkBarCode)
        {
            MySqlConnection conn = null;
            MySqlTransaction saveBarTrans = null;
            int insCnt = 0;
            try
            {
                LogUtil.Debug("删除机芯机壳重复数据：" + jxjkBarCode.JkBarcodeId + " " + jxjkBarCode.JkBarcodeId);
                conn = db.getConnection();
                conn.Open();
                saveBarTrans = conn.BeginTransaction();

                string jxExistSql = "select count(1) from jxjk_barcode where jx_barcodeID = @jx_barcodeID and mark_repair='合格' ";
                string jkExistSql = "select count(1) from jxjk_barcode where jk_barcodeID = @jk_barcodeID and mark_repair='合格' ";
                string delJxSql = "delete from jxjk_barcode where jx_barcodeID = @jx_barcodeID and mark_repair='合格'";
                string delJkSql = "delete from jxjk_barcode where jk_barcodeID = @jk_barcodeID and mark_repair='合格'";
                MySqlParameter existJxParam = new MySqlParameter("@jx_barcodeID", jxjkBarCode.JxBarcodeId);
                object existJxRes = db.ExecuteScalar(conn, CommandType.Text, jxExistSql, new MySqlParameter[] { existJxParam });
                int delJx = 0;
                if (Convert.ToInt32(existJxRes) > 0)
                {
                    delJx = db.ExecuteNonQuery(saveBarTrans, CommandType.Text, delJxSql, existJxParam);
                }
                LogUtil.Debug("删除机芯重复数据：" + delJx);

                MySqlParameter existJkParam = new MySqlParameter("@jk_barcodeID", jxjkBarCode.JkBarcodeId);
                object existJkRes = db.ExecuteScalar(conn, CommandType.Text, jkExistSql, new MySqlParameter[] { existJkParam });
                int delJk = 0;
                if (Convert.ToInt32(existJkRes) > 0)
                {
                    delJk = db.ExecuteNonQuery(saveBarTrans, CommandType.Text, delJkSql, existJkParam);
                }
                LogUtil.Debug("删除机壳重复数据：" + delJk);

                string insJXJKSql = "INSERT INTO jxjk_barcode (jx_barcodeID, jk_barcodeID, vavle_barcodeID, Operator_Name, OpDt, mark_repair, up_shell, down_shell,frame1,frame2) VALUES (@jx_barcodeID, @jk_barcodeID, @vavle_barcodeID, @Operator_Name, sysdate(), @mark_repair, @UpShellCode, @DownShellCode,@frame1,frame2)";
                LogUtil.Info("机芯机壳扫码信息插入数据：" + jxjkBarCode.JkBarcodeId + " " + jxjkBarCode.JkBarcodeId);
                MySqlParameter[] sqlParams = new MySqlParameter[9];
                sqlParams[0] = new MySqlParameter("@jx_barcodeID", jxjkBarCode.JxBarcodeId);
                sqlParams[1] = new MySqlParameter("@jk_barcodeID", jxjkBarCode.JkBarcodeId);
                sqlParams[2] = new MySqlParameter("@vavle_barcodeID", jxjkBarCode.VavleBarcodeId);
                sqlParams[3] = new MySqlParameter("@Operator_Name", jxjkBarCode.OperatorName);
                sqlParams[4] = new MySqlParameter("@mark_repair", "合格");
                sqlParams[5] = new MySqlParameter("@UpShellCode", jxjkBarCode.UpShellCode);
                sqlParams[6] = new MySqlParameter("@DownShellCode", jxjkBarCode.DownShellCode);
                sqlParams[7] = new MySqlParameter("@frame1", jxjkBarCode.frame1Code);
                sqlParams[8] = new MySqlParameter("@frame2", jxjkBarCode.frame2Code);
                insCnt = db.ExecuteNonQuery(saveBarTrans, CommandType.Text, insJXJKSql, sqlParams);     
                LogUtil.Info("机芯机壳扫码信息插入数据：" + insCnt);

                // 提交
                saveBarTrans.Commit();
                return Consts.RET_SUCCESS;
            }
            catch (Exception ex)
            {
                LogUtil.Error(ex);
                if (saveBarTrans != null)
                {
                    saveBarTrans.Rollback();
                }
            }
            finally
            {
                try
                {
                    if (conn != null && conn.State != ConnectionState.Closed)
                    {
                        conn.Close();
                        conn.Dispose();
                    }
                }
                catch (Exception e)
                {
                    LogUtil.Error(e);
                }
            }
            return Consts.RET_FAIL;
        }

        /// <summary>
        /// 更新返修状态
        /// </summary>
        /// <param name="jxjkBarCode"></param>
        /// <returns></returns>
        public int updateJKRepaireStatus(JXJKBarCodeInfo jxjkBarCode)
        {
            MySqlConnection conn = null;
            MySqlTransaction updJKTrans = null;
            try
            {
                conn = db.getConnection();
                conn.Open();
                updJKTrans = conn.BeginTransaction();

                string jkExistSql = "select count(1) from jxjk_barcode where jk_barcodeID = @jk_barcodeID and mark_repair='合格' ";
                string updJkSql = "update jxjk_barcode set mark_repair='返修',OpDt=sysdate()  where jk_barcodeID = @jk_barcodeID and mark_repair='合格' ";

                MySqlParameter existJkParam = new MySqlParameter("@jk_barcodeID", jxjkBarCode.JkBarcodeId);
                object existJkRes = db.ExecuteScalar(conn, CommandType.Text, jkExistSql, new MySqlParameter[] { existJkParam });
                if (Convert.ToInt32(existJkRes) > 0)
                {
                    db.ExecuteNonQuery(updJKTrans, CommandType.Text, updJkSql, new MySqlParameter[] { existJkParam });
                }
                else
                {
                    updJKTrans.Rollback();
                    return Consts.RET_NOTEXIST;
                }

                // 提交
                updJKTrans.Commit();
                return Consts.RET_SUCCESS;
            }
            catch (Exception ex)
            {
                LogUtil.Error(ex);
                if (updJKTrans != null)
                {
                    updJKTrans.Rollback();
                }
            }
            finally
            {
                try
                {
                    if (conn != null && conn.State != ConnectionState.Closed)
                    {
                        conn.Close();
                        conn.Dispose();
                    }
                }
                catch (Exception e)
                {
                    LogUtil.Error(e);
                }
            }
            return Consts.RET_FAIL;
        }

        /// <summary>
        /// 通过机壳获取机芯
        /// </summary>
        /// <param name="jkCode"></param>
        /// <returns></returns>
        public string getJXCodeByJK(String jkCode)
        {
            MySqlParameter param = new MySqlParameter("@jk_barcodeID", jkCode);
            string getJxSql = "select jx_barcodeID from jxjk_barcode where jk_barcodeID=@jk_barcodeID ";
            object JxCode = db.ExecuteScalar(CommandType.Text, getJxSql, new MySqlParameter[] { param });
            if (JxCode == null)
            {
                return "";
            }
            else
            {
                return Convert.ToString(JxCode);
            }
        }

    }
}
